package com.csii.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.csii.user.entity.SysRoleGroupRelation;
import com.csii.user.mapper.SysRoleGroupRelationMapper;
import com.csii.user.service.ISysRoleGroupRelationService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: sys_role_group_relation
 * @Author: power-stdio
 * @Date:   2021-04-27
 * @Version: V1.0
 */
@Service
public class SysRoleGroupRelationServiceImpl extends ServiceImpl<SysRoleGroupRelationMapper, SysRoleGroupRelation> implements ISysRoleGroupRelationService {

    @Override
    public void saveBatch(String groupId, List<String> roleIdList) {
        List<SysRoleGroupRelation> orgRoleIds=baseMapper.selectList(new LambdaQueryWrapper<>(SysRoleGroupRelation.class).eq(SysRoleGroupRelation::getGroupId,groupId));
        List<String> removeIds=new ArrayList<>();
        List<SysRoleGroupRelation> addList=new ArrayList<>();
        for(SysRoleGroupRelation s: orgRoleIds)
        {
            if(!roleIdList.contains(s.getRoleId()))
                removeIds.add(s.getId());
        }
        for(String r:roleIdList)
        {
            boolean addFlag=true;
            for(SysRoleGroupRelation sr: orgRoleIds)
            {
                if(sr.getRoleId().equals(r))
                {
                    addFlag=false;
                    break;
                }
            }
            if(addFlag)
                addList.add(new SysRoleGroupRelation(groupId,r));
        }
        saveBatch(addList);
        removeByIds(removeIds);
    }
}
